import { Observable } from "../internal/Observable";
import { distinctUntilChanged } from "../internal/operators/distinctUntilChanged";
import { filter } from "../internal/operators/filter";
import { EveryFrame } from './CreatorSchedule';

export function Watch(selector: () => any) {
    return new Observable(observer => {
        let _sub = EveryFrame().subscribe(_ => {
            observer.next(selector());
        });
        return () => _sub.unsubscribe();
    });
}

export function WatchIgnoreNullUntilChanged(selector: () => any) {
    return Watch(selector).pipe(filter(_ => _ != null), distinctUntilChanged());
}